package group.rober.saber.jdbc.query;

import org.apache.commons.lang3.Validate;

/**
 * MySQL分页实现
 * Created by tisir<yangsong158@qq.com> on 2017-05-28
 */
public class MySqlPaginationHandler implements SqlPaginationHandler {
    /**
     * SQL查询列表分页处理SQL部分
     * @param sql 原始SQL查询
     * @param index 页码索引(从0开始),第几页
     * @param size 每页大小
     * @return
     */
    public String queryListSql(String sql, int index, int size) {
//        Validate.isTrue(index<=-1,"pagnation index must be greater than 0,index={0}",index);
//        Validate.isTrue(size==0,"The paging size is not allowed to be 0",size);
        Validate.notBlank(sql,"pagination query SQL is not allowed to empty");

        StringBuffer sbSql = new StringBuffer(sql);
        if(size > 0){
            Validate.isTrue(index>-1,"pagination index must be greater than -1,index=%1$d",index);
            int rowIndex = index*size;
            sbSql.append(" LIMIT ").append(rowIndex);
            sbSql.append(",").append(size);
        }

        return sbSql.toString();
    }
}
